package com.xxyw.youjingmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxyw.youjingmall.product.dao.AttrAttrgroupRelationDao;
import com.xxyw.youjingmall.product.dao.AttrDao;
import com.xxyw.youjingmall.product.entity.AttrAttrgroupRelationEntity;
import com.xxyw.youjingmall.product.entity.AttrEntity;
import com.xxyw.youjingmall.product.service.AttrAttrgroupRelationService;
import com.xxyw.youjingmall.product.service.AttrService;
import com.xxyw.youjingmall.product.vo.AttrGroupAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxyw.common.utils.PageUtils;
import com.xxyw.common.utils.Query;

import com.xxyw.youjingmall.product.dao.AttrGroupDao;
import com.xxyw.youjingmall.product.entity.AttrGroupEntity;
import com.xxyw.youjingmall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private final AttrAttrgroupRelationService attrAttrgroupRelationService;

    public AttrGroupServiceImpl(AttrAttrgroupRelationDao attrAttrgroupRelationDao, AttrDao attrDao, AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService) {
        this.attrAttrgroupRelationDao = attrAttrgroupRelationDao;
        this.attrDao = attrDao;
        this.attrAttrgroupRelationService = attrAttrgroupRelationService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // select * from pms_attr_group where catelog_id = catelogId and (attr_group_id = key or attr_group_name like key)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((w) -> {
                w.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrGroupId) {

        // 从 relation 表中查询所有的 attr
        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));

        List<Long> attrIdList = relationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        if (attrIdList.isEmpty()) {
            return null;
        }

        List<AttrEntity> attrEntityList = attrDao.selectBatchIds(attrIdList);

        return attrEntityList;

    }

    @Override
    public void deleteRelation(List<AttrAttrgroupRelationEntity> relationList) {
        attrAttrgroupRelationDao.deleteBatchRelation(relationList);
    }

    @Override
    public List<AttrGroupAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        // 1. 根据 catelogId 查询所有的 attrGroup
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 2. 根据 attrGroup 查询所有的 attr
        List<AttrGroupAttrsVo> collect = attrGroupEntityList.stream().map(attrGroup -> {
            AttrGroupAttrsVo attrGroupAttrsVo = new AttrGroupAttrsVo();
            BeanUtils.copyProperties(attrGroup, attrGroupAttrsVo);
            List<AttrEntity> attrEntityList = getRelationAttr(attrGroup.getAttrGroupId());
            attrGroupAttrsVo.setAttrs(attrEntityList);
            return attrGroupAttrsVo;
        }).collect(Collectors.toList());

        return collect;
    }


}